package cn.edu.sxw.web.server.api;

import cn.edu.njuit.web.server.tools.ImageTool;
import cn.edu.sxw.web.server.service.BaiduAiService;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.swing.*;
import java.io.IOException;

@WebServlet(urlPatterns = "/api/ai-face")
@MultipartConfig
public class FaceApi extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Part image = req.getPart("image");
        ImageTool imageTool = new ImageTool();
        //将图片转换为base64
        String imageBase64 = imageTool.image2Base64(image.getInputStream());

        //调用百度ai代码，
        BaiduAiService baiduAiService = new BaiduAiService();
        JSONObject res = baiduAiService.checkFaces(imageBase64);
        System.out.println(res);

        //进行响应
        resp.setContentType("application/json;charset=utf8");
        resp.getWriter().write(res.toString());
    }
}
